package com.yanqu.road.dao.impl.activity.worldcup;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.worldcup.WorldCupPlayerConfig;
import com.yanqu.road.entity.activity.worldcup.WorldCupSkillConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WorldCupConfigDaoImpl extends TempDao {

    private static String getWorldCupPlayerConfigSql = "select * from `t_s_activity_worldcup_player` where `activityId`=? ORDER BY `id` ASC";

    public Map<Integer, WorldCupPlayerConfig> getWorldCupPlayerConfigList(int activityId) {
        Map<Integer, WorldCupPlayerConfig> configMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWorldCupPlayerConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WorldCupPlayerConfig config = new WorldCupPlayerConfig();
                config.setActivityId(rs.getInt("activityId"));
                config.setQuality(rs.getInt("id"));
                config.setExpAdd(rs.getInt("exp"));
                config.setRewards(rs.getString("rewards"));
                config.setIcon(rs.getString("icon"));
                config.setName(rs.getString("name"));
                configMap.put(config.getQuality(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }


    private static String getWorldCupSkillConfigSql = "select * from `t_s_activity_worldcup_skill` where `activityId`=? ORDER BY `level` ASC";

    public Map<Integer, List<WorldCupSkillConfig>> getWorldCupSkillConfigList(int activityId) {
        Map<Integer, List<WorldCupSkillConfig>> configMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWorldCupSkillConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WorldCupSkillConfig config = new WorldCupSkillConfig();
                config.setActivityId(rs.getInt("activityId"));
                config.setType(rs.getInt("type"));
                config.setLevel(rs.getInt("level"));
                config.setIcon(rs.getString("icon"));
                config.setName(rs.getString("name"));
                config.setDesc(rs.getString("desc"));
                config.setCost(rs.getInt("cost"));
                config.setRecruitInfo(rs.getString("recruitInfo"));
                if (configMap.containsKey(config.getType())) {
                    configMap.get(config.getType()).add(config);
                } else {
                    List<WorldCupSkillConfig> levelConfig = new ArrayList<>();
                    levelConfig.add(config);
                    configMap.put(config.getType(), levelConfig);
                }
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }
}
